package active;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

/**
 * @author 陈华旺
 * @version 1.0
 * @date 2023年11月21日 11:43
 * @Description TODO
 */
@WebServlet("/test06")
public class Test06Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        getParameter(key) 通过对http请求的解析，获取由页面传递的数据参数
//                          页面传递的参数以 key 和 value 两部分组成，该方法通过传入key值获取value数据
//        getParameterValues(key) 通过http请求的解析，对解析结果中key相同的所有参数以数组的形式进行记录
        System.out.println( "名称：" + req.getParameter("name") );
        System.out.println( "年龄：" + req.getParameter("age") );
        System.out.println( "性别：" + req.getParameter("sex") );
//        System.out.println( "技能：" + req.getParameter("skills") );
        String[] skills = req.getParameterValues("skills");
        System.out.println( "技能：" + Arrays.toString(skills));
        System.out.println( "学历：" + req.getParameter("edu") );
        System.out.println( "简介：" + req.getParameter("info") );

        System.out.println("===============");
//        getParameterMap() 通过对http请求的解析，以参数名作为key，参数值作为value数组的元素进行定义
        Map<String, String[]> map = req.getParameterMap();
        Set<String> keys = map.keySet();
        for (String k:keys) {
            System.out.println( k + ":" + Arrays.toString( map.get(k) ) );
        }

        System.out.println("===============");
        Enumeration<String> keyIt = req.getParameterNames();
        while ( keyIt.hasMoreElements() ){
            String key = keyIt.nextElement();
            System.out.println( key + ":" + req.getParameter(key) );
        }

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.println("result-通过 test06Request.html 测试参数");
    }
}
